<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

//产品表
class Product extends Admin
{
    public function index()
    {
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        if($this->request->isPost())
        {
            $post = input('post.');
            $where = [];
            //订单编号
            $where['orderid'] = $post['orderid']??'';
            //买家
            $where['buyer'] = $post['user']??'';
            //状态
            $where['status'] = $post['status']??'';
            $wheres =[];
            foreach ($where as $k=>$v){
                if(strlen($v)>0){
                    if($k !== 'time') $wheres[] = [$k,'=',$v];
                }
            }
            if($wheres){
                $list = model($this->request->controller())->with('classs')->where($wheres)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where($wheres)->select()->toArray());
            }else{
                $list = model($this->request->controller())->with('classs')->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }else{
            $list = model($this->request->controller())->with('classs')->paginate($limit);
            $page = $list->render();
            $list = $list->toArray()['data'];
            $count = count(model($this->request->controller())->select()->toArray());
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        return $this->fetch();
    }
    public function group()
    {
        return $this->fetch();
    }
    public function status()
    {
        if($this->request->isPost()){
            $post = input('post.');
            if($post['status'] == 10){
                $post['status'] = 20;
            }else{
                $post['status'] = 10;
            }
            try{
                $model = model($this->request->controller())->save(['status'=>$post['status']],['id'=>$post['id']]);
            }catch (\Exception $e){
                return json_encode($e->getMessage());
            }
            return json_encode($model);
        }
    }
    public function imagesdt()
    {
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( 'upload/dt/');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $info->getSaveName();
        }else{
            // 上传失败获取错误信息
            return json_encode($file->getError());
        }
        return json_encode('/upload/dt/' . $info->getSaveName());
    }
}
